package com.cskaoyan.controller.keyword;

import com.cskaoyan.bean.MallKeyword;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.keyword.PageBO2;
import com.cskaoyan.model.bo.keyword.create.KeywordCreateBO;
import com.cskaoyan.model.bo.keyword.delete.KeywordDeleteBO;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.keyword.KeywordVO;
import com.cskaoyan.model.vo.keyword.create.KeywordCreateVO;
import com.cskaoyan.service.keyword.KeywordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: project-market
 * @description: 关键词
 * @author: shiyue
 * @create: 2021/11/30 20:24
 **/

@RestController
@RequestMapping("admin/keyword")
public class KeywordController {

    @Autowired
    KeywordService keywordService;

    @RequiresPermissions("admin:keyword:list")
    @GetMapping("list")
    public BaseRespVo getKeyword(PageBO2 pageBo) {
        KeywordVO keywordVO = keywordService.getKeyword(pageBo);
        return BaseRespVo.ok(keywordVO);
    }

    @RequiresPermissions("admin:keyword:create")
    @PostMapping("create")
    public BaseRespVo createKeyword(@RequestBody KeywordCreateBO bo) {

        if (bo.getKeyword() == null || bo.getUrl() == null || bo.getIsHot() == null || bo.getIsDefault() == null) {
            return BaseRespVo.manual(611, "参数不合法", null);
        }

        int id = keywordService.createKeyword(bo);
        MallKeyword mallKeyword = keywordService.getOneKeyword(id);
        return BaseRespVo.ok(mallKeyword);
    }

    @RequiresPermissions("admin:keyword:delete")
    @PostMapping("delete")
    public BaseRespVo deleteKeyword(@RequestBody KeywordDeleteBO keywordDeleteBO){
        int row=keywordService.deleteKeyword(keywordDeleteBO.getId());
        return BaseRespVo.ok();
    }

    @RequiresPermissions("admin:keyword:update")
    @PostMapping("update")
    public BaseRespVo updateKeyword(@RequestBody KeywordDeleteBO keywordDeleteBO){
        int row=keywordService.updateKeyword(keywordDeleteBO);
        return BaseRespVo.ok();
    }
}
